package com.aboverock.module.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aboverock.common.helper.SqlHelper;
import com.aboverock.module.system.constant.SystemConstants;
import com.aboverock.module.system.domain.Role;
import com.aboverock.module.system.domain.UserRole;
import com.aboverock.module.system.mapper.UserRoleMapper;
import com.aboverock.module.system.service.RoleService;
import com.aboverock.module.system.service.UserRoleService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Rock Wang
 * @since 2018-04-10
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    @Autowired
    private RoleService roleService;

    @Override
    public List<Role> getRolesByUserId(String userId) {
        return baseMapper.selectRolesByUserId(userId);
    }

    @Override
    public List<String> getUserIdsByRoleId(String roleId) {
        return baseMapper.selectUserIdsByRoleId(roleId);
    }

    @Override
    public boolean removeByRoleIdAndUserIds(String roleId, List<String> userIds) {
        return SqlHelper.delBool(baseMapper.removeByRoleIdAndUserIds(roleId, userIds));
    }

    @Override
    public boolean removeByUserIds(List<String> userIds) {
        return SqlHelper.delBool(baseMapper.removeByUserIds(userIds));
    }

    @Override
    public boolean addCommonRole(String userId) {
        Role commonRole = roleService.getByRoleCode(SystemConstants.COMMON_ROLE_CODE);
        if (null == userId || null == commonRole) {
            return false;
        }
        UserRole userRole = new UserRole();
        userRole.setRoleId(commonRole.getId());
        userRole.setUserId(userId);
        return this.save(userRole);
    }

}
